#!/usr/bin/python
import os
import glob
import sys
import bz2
import zlib
import pylzma
import subprocess

# def ncd_mpg(x, y):
#     return float(C(x,y) - min(os.path.getsize(x+".mpg"), os.path.getsize(y+".mpg"))) / max(os.path.getsize(x+".mpg"), os.path.getsize(y+".mpg"))

# def ncd_mpg2(x, y):
#     return (float(mpg_size(x,y) + mpg_size(y,x))/float(mpg_size(x,x) + mpg_size(y,y))) - 1

def mpg_size(fname1, fname2, base):
    return os.path.getsize(os.path.join(base, fname1+"_"+fname2+".mpg"))

def zc(o):
    return len(zlib.compress(o, 9))

def bc(o):
    return len(bz2.compress(o, 9))

def lc(o):
    return len(pylzma.compress(o, 9))

def ncd_zlib(x, y):
    fx = open(x).read()
    fy = open(y).read()
    return float(zc(fx+fy) - min(zc(fx), zc(fy))) / max(zc(fx), zc(fy))

def ncd_lzma(x, y):
    fx = open(x).read()
    fy = open(y).read()
    return float(lc(fx+fy) - min(lc(fx), lc(fy))) / max(lc(fx), lc(fy))

def ncd_bz2(x, y):
    fx = open(x).read()
    fy = open(y).read()
    return float(bc(fx+fy) - min(bc(fx), bc(fy))) / max(bc(fx), bc(fy))

def ncd_mpg(x, y):
    x_basename = ""
    for s in os.path.basename(x).split('.')[:-1]:
        x_basename += s
    y_basename = ""
    for s in os.path.basename(y).split('.')[:-1]:
        y_basename += s
    filenames = ['/tmp/confusion_mpgs/' + s + '.mpg' for s in [x_basename+"_"+y_basename, y_basename+"_"+x_basename, x_basename+"_"+x_basename, y_basename+"_"+y_basename]]
    if not all(map(os.path.exists, filenames)):
        if not (os.path.exists, '/tmp/confusion_mpgs'):
            subprocess.call(["mkdir", "/tmp/confusion_mpgs"])
        subprocess.call(["convert", "-quality", "10", x, y, filenames[0]])
        subprocess.call(["convert", "-quality", "10", y, x, filenames[1]])
        subprocess.call(["convert", "-quality", "10", x, x, filenames[2]])
        subprocess.call(["convert", "-quality", "10", y, y, filenames[3]])
    return ncd_mpg_helper(x_basename,y_basename, '/tmp/confusion_mpgs')

def ncd_mpg_helper(x, y, base):
    return (float(mpg_size(x,y,base) + mpg_size(y,x,base))/float(mpg_size(x,x,base) + mpg_size(y,y,base))) - 1

def closest(x, seq, ncd_measure):
    lowest_ncd = None
    lowest_pair = None
    for y in seq:
        if lowest_ncd == None or lowest_ncd > ncd_measure(x,y):
            lowest_ncd = ncd_measure(x,y)
            lowest_pair = y
    return (lowest_pair, lowest_ncd)


# s1 = sorted(glob.glob(os.path.join(sys.argv[1], "*.bmp")))
# s2 = sorted(glob.glob(os.path.join(sys.argv[2], "*.bmp")))

s1 = sorted(glob.glob(os.path.join(sys.argv[1], "*.ch")))
s2 = sorted(glob.glob(os.path.join(sys.argv[2], "*.ch")))

for measure in [ncd_zlib, ncd_bz2, ncd_lzma]:
#for measure in [ncd_mpg]:
    print "stats for", measure.__name__
    print "     \t",
    for h in s2:
        print h.split("/")[1].split(".")[0]+"\t",
    print
    for b1 in s1:
        print b1.split("/")[1].split(".")[0]+"\t",
        for b2 in s2:
            print '%.4f' % measure(b1, b2) + "\t",
        print
        
    count = 0
    print "printing closest glyph, not of same font"
    for s in s1:
        closest_glyph = closest(s,s2,measure)
        if closest_glyph[0].split("/")[1].split("_")[0] == s.split("/")[1].split("_")[0] :
            count += 1
        print s, closest_glyph
    print count, "of", len(s1), "match"

    count = 0
    for s in s2:
        closest_glyph = closest(s,s1,measure)
        if closest_glyph[0].split("/")[1].split("_")[0] == s.split("/")[1].split("_")[0] :
            count += 1
        print s, closest_glyph
    print count, "of", len(s2), "match"


    print "printing closest glyph, could be same font"
    count = 0
    for s in s1:
        closest_glyph = closest(s, filter(lambda l: l != s , s1) + s2,measure)
        if closest_glyph[0].split("/")[1].split("_")[0] == s.split("/")[1].split("_")[0] :
            count += 1
        print s, closest_glyph

    print count, "of", len(s1), "match"

    count = 0
    for s in s2:
        closest_glyph = closest(s, filter(lambda l: l != s , s2) + s1,measure)
        if closest_glyph[0].split("/")[1].split("_")[0] == s.split("/")[1].split("_")[0] :
            count += 1
        print s, closest_glyph
    print count, "of", len(s2), "match"
